package com.press.services;

import com.press.bean.ContentBean;
import com.press.bean.MappingBean;
import com.press.bean.TaxonomyBean;
import com.press.mapper.ContentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.text.AbstractDocument;
import javax.swing.text.html.parser.ContentModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/10/3.
 */
@Service("contentService")
public class ContentService {
    @Resource(name = "contentMapper")
    private ContentMapper contentMapper;

    public ContentMapper getContentMapper() {
        return contentMapper;
    }

    public void setContentMapper(ContentMapper contentMapper) {
        this.contentMapper = contentMapper;
    }

    public List<ContentBean> getContent(){
        return contentMapper.getContent();
    }
    public boolean addContent(Map map){
        String[] typeArr = new String[20];
        map.put("content_id","11");
        int content_id;
        typeArr = (String[]) map.get("arr");
       // System.out.println("数组typeArr"+typeArr);
        //System.out.println(typeArr.length);
        if(typeArr.length==1){
            Map map2 = new HashMap<>();
            map2.put("type_id",typeArr[0]);
            if("draft".equals(map.get("tip"))){
                content_id = contentMapper.draftContent(map);
            }else if("add".equals(map.get("tip"))){
                content_id = contentMapper.addContent(map);
            }
            map2.put("content_id",map.get("content_id"));
           return contentMapper.addContentAndType(map2)>0;
        }else{
            Map map2 = new HashMap<>();
            int result = 0;
            if("draft".equals(map.get("tip"))){
                content_id = contentMapper.draftContent(map);
            }else if("add".equals(map.get("tip"))){
                content_id = contentMapper.addContent(map);
            }
            map2.put("content_id",map.get("content_id"));
            for(int i = 0;i<typeArr.length;i++){
                map2.put("type_id",typeArr[i]);
               result= contentMapper.addContentAndType(map2);
            }
            return result>0;
        }
    }
    public boolean moveToBin(Map map){
        return contentMapper.moveToBin(map)>0;
    }
    public boolean moveToNoBin(Map map){
        return contentMapper.moveToNoBin(map)>0;
    }
    public String getBinCount(){
        String  count = contentMapper.getBinCount();
       //System.out.println("bin"+count);
        return count;
    }
    public String getNoBinCount(){
        String  count = contentMapper.getNoBinCount();
       // System.out.println("bin"+count);
        return count;
    }
    public List <MappingBean> getmapping(){
        return contentMapper.getMapping();
    }
    public List<ContentBean> getBinContent(){
        return contentMapper.getBinContent();
    }
    public boolean delete(int content_id){
        if(contentMapper.deteleContentAndType(content_id)>0){
            return contentMapper.deleteContent(content_id)>0;
        }else{
            return false;
        }
    }
    public List<TaxonomyBean> getAllType(){
        return contentMapper.getAllType();
    }
    public ContentBean getContentDetail(String content_id){
        return contentMapper.getContentDetail(content_id);
    }
    public List<TaxonomyBean> getTypeById(String content_id){
        return contentMapper.getTypeById(content_id);
    }
    public boolean deleteMapping(String content_id){
        return contentMapper.deleteMapping(content_id)>0;
    }
    public boolean updateContent(Map map) {
        String[] typeArr = new String[20];
        typeArr = (String[]) map.get("arr");
        int update=0;
        //System.out.println("数组长度"+typeArr.length);
        if("save".equals((String)map.get("remark"))){
            update = contentMapper.updateContent(map);
        }else if("pub".equals((String)map.get("remark"))){
            update = contentMapper.updateContentToNoBin(map);
            System.out.println("pub"+update);
        }
        if (typeArr.length == 1 && update > 0) {
           // System.out.println("执性=1");
            Map map2 = new HashMap<>();
            map2.put("type_id", typeArr[0]);
            map2.put("content_id", map.get("content_id"));
            return contentMapper.addContentAndType(map2) > 0;
        } else if (typeArr.length != 1 && update > 0) {
           // System.out.println("执性>1");
            Map map2 = new HashMap<>();
            int result = 0;
            map2.put("content_id", map.get("content_id"));
            for (int i = 0; i < typeArr.length; i++) {
                map2.put("type_id", typeArr[i]);
               // System.out.println("执性"+map2.get("type_id"));
                result = contentMapper.addContentAndType(map2);
            }
            return result > 0;
        }
        return false;
    }
    public List<ContentBean> getDraftContent(){
        return contentMapper.getDraftContent();
    }
    public String getDraftCount(){
        return contentMapper.getDraftCount();
    }
    public List<ContentBean> getSearchList(String content_title,String tip){
        if("bin".equals(tip)){
            return contentMapper.getSearchBinList(content_title);
        }else if("noBin".equals(tip)){
            return contentMapper.getSearchNoBinList(content_title);
        }else if("draft".equals(tip)){
            return contentMapper.getSearchDraftList(content_title);
        }
        return null;
    }
}
